package com.winit.openapi.wh.builder;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.common.query.Searchable;
import com.winit.oms.spi.inbound.command.InboundOrderMergeCancelCommand;
import com.winit.oms.spi.inbound.command.InboundOrderMergeCommand;
import com.winit.oms.spi.inbound.command.QuerySubInboundOrderByMainOrderCommand;
import com.winit.oms.spi.inbound.command.SearchInboundOrderCommand;
import com.winit.oms.spi.inbound.vo.InboundEnumConstant;
import com.winit.oms.spi.inbound.vo.InboundOrderVo;

/**
 * 海外仓入库单构造器
 * 
 * @version <pre>
 * Author	  Version		Date		Changes
 * dinghua.an	1.0  		2015年11月10日 	Created
 *
 * </pre>
 * @since 1.
 */
public class InboundOrderMergeBuilder extends InboundBaseBuilder {

    /**
     * 默认分页页数1
     */
    private static final int DEFAULT_PAGE_NO   = 1;
    /**
     * 默认每页记录数100
     */
    private static final int DEFAULT_PAGE_SIZE = 100;
    /**
     * 默认每页记录数1
     */
    private static final int DEFAULT_SIZE      = 1;

    /**
     * 构建查询信息
     * 
     * @param json
     * @return
     */
    public static SearchInboundOrderCommand buildQueryParam(JSONObject json) {
        SearchInboundOrderCommand command = new SearchInboundOrderCommand();
        String entryWhType = null;
        if (json.containsKey("orderType") && StringUtils.isNoneBlank(json.getString("orderType"))) {
            entryWhType = json.getString("orderType");
        }
        // else {
        // // 缺省值为SD
        // entryWhType = InboundEnumConstant.OrderType.STANDARD.getType();
        // }
        command.setEntryWhType(entryWhType);
        command.setMainNo(json.getString("mainNo"));
        Searchable searchable = null;
        if (json.containsKey("subsidiaryNo") && StringUtils.isNotBlank(json.getString("subsidiaryNo"))) {
            command.setSubsidiaryNo(json.getString("subsidiaryNo"));
            searchable = Searchable.newSearchable();
            searchable.setPage(DEFAULT_PAGE_NO, DEFAULT_SIZE);
        } else {
            // 如果子单号为空，则按照分页或者列表的方式查询
            String queryMode = json.getString("queryMode");
            if ("P".equals(queryMode)) {
                searchable = buildSearchablePageParams(json);
            } else {
                // 列表模式查询的时候默认查询100条
                searchable = Searchable.newSearchable();
                searchable.setPage(DEFAULT_PAGE_NO, DEFAULT_PAGE_SIZE);
                queryMode = "L";
            }
            command.setQueryMode(queryMode);
        }
        command.setSearchable(searchable);
        return command;
    }

    public static List<Map<String, Object>> buildOrderList(List<InboundOrderVo> datalist) {
        List<Map<String, Object>> rslist = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = null;
        for (InboundOrderVo order : datalist) {
            map = buildMergeOrderBuild(order);
            rslist.add(map);
        }
        return rslist;
    }

    /**
     * 构建合并订单command
     * 
     * @param json
     * @return
     */
    public static InboundOrderMergeCommand buildMergeCommand(JSONObject json) {
        InboundOrderMergeCommand command = new InboundOrderMergeCommand();
        command.setMainOrderNo(json.getString("mainNo"));

        List<String> subOrderNoList = new ArrayList<String>();
        JSONArray list = json.getJSONArray("subsidiaryNo");
        for (Object object : list) {
            JSONObject orderNoObj = (JSONObject) object;
            if (orderNoObj.containsKey("orderNo") && StringUtils.isNotBlank(orderNoObj.getString("orderNo"))) {
                subOrderNoList.add(orderNoObj.getString("orderNo"));
            }
        }
        command.setSubOrderNoList(subOrderNoList);
        return command;
    }

    /**
     * 构建取消合并command
     * 
     * @param json
     * @return
     */
    public static InboundOrderMergeCancelCommand buildMergeCancelCommand(JSONObject json) {
        InboundOrderMergeCancelCommand command = new InboundOrderMergeCancelCommand();
        command.setCancelType(json.getString("cancelType"));
        if (InboundEnumConstant.OrderMergeType.MAIN.getCode().equals(json.getString("cancelType"))) {
            command.setMainNo(json.getString("mainNo"));
        } else {
            List<String> subOrderNoList = new ArrayList<String>();
            JSONArray list = json.getJSONArray("subOrderNoList");
            for (Object object : list) {
                JSONObject orderNoObj = (JSONObject) object;
                if (orderNoObj.containsKey("surOrderNo") && StringUtils.isNotBlank(orderNoObj.getString("surOrderNo"))) {
                    subOrderNoList.add(orderNoObj.getString("surOrderNo"));
                }
            }
            command.setOrderNoList(subOrderNoList);
        }
        return command;
    }

    /**
     * 构建合并订单command
     * 
     * @param json
     * @return
     */
    public static QuerySubInboundOrderByMainOrderCommand buildQuerySubOrderByMainOrderCommand(JSONObject json) {
        QuerySubInboundOrderByMainOrderCommand command = new QuerySubInboundOrderByMainOrderCommand();
        command.setMainOrderNo(json.getString("mainNo"));
        String queryMode = json.getString("queryMode");
        Searchable searchable = null;
        if ("P".equals(queryMode)) {
            searchable = buildSearchablePageParams(json);
        } else {
            queryMode = "L";
            // 列表模式查询的时候默认查询100条
            searchable = Searchable.newSearchable();
            searchable.setPage(DEFAULT_PAGE_NO, DEFAULT_PAGE_SIZE);
        }
        command.setQueryMode(queryMode);
        command.setSearchable(searchable);
        return command;
    }

    private static Map<String, Object> buildMergeOrderBuild(InboundOrderVo inboundOrder) {
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("orderNo", inboundOrder.getOrderNo());
        map.put("inspectionWhName", inboundOrder.getInspectionWhName());
        map.put("destinationWHName", inboundOrder.getDestWhName());
        map.put("winitProductName", inboundOrder.getWinitProductName());
        map.put("logisticsPlanName", inboundOrder.getLogisticsPlanName());
        map.put("transportType", inboundOrder.getTransprotType());
        map.put("orderDate", inboundOrder.getOrderDate());
        map.put("mainOrderNo", inboundOrder.getMainOrderNo());

        BigDecimal weight = inboundOrder.getOrderWeight();
        if (weight == null || BigDecimal.ZERO.compareTo(weight) >= 0) {
            weight = BigDecimal.ZERO;
        }
        map.put("sellerWeight", weight);
        BigDecimal acthalWeight = inboundOrder.getActualOrderWeight();
        if (acthalWeight == null || BigDecimal.ZERO.compareTo(acthalWeight) >= 0) {
            acthalWeight = BigDecimal.ZERO;
        }
        map.put("totalWeight", acthalWeight);

        BigDecimal volumn = inboundOrder.getOrderVolume();
        if (volumn == null || BigDecimal.ZERO.compareTo(volumn) >= 0) {
            volumn = BigDecimal.ZERO;
        }
        map.put("sellerVolume", volumn);
        BigDecimal acthalVolumn = inboundOrder.getActualOrderVolume();
        if (acthalVolumn == null || BigDecimal.ZERO.compareTo(acthalVolumn) >= 0) {
            acthalVolumn = BigDecimal.ZERO;
        }
        map.put("totalVolume", acthalVolumn);

        BigDecimal sellerChargeWeight = inboundOrder.getOrderChargeWeight();
        if (sellerChargeWeight == null || BigDecimal.ZERO.compareTo(sellerChargeWeight) >= 0) {
            sellerChargeWeight = BigDecimal.ZERO;
        }
        map.put("sellerChargeWeight", sellerChargeWeight);
        BigDecimal acthalChargeWeight = inboundOrder.getActualOrderChargeWeight();
        if (acthalChargeWeight == null || BigDecimal.ZERO.compareTo(acthalChargeWeight) >= 0) {
            acthalChargeWeight = BigDecimal.ZERO;
        }
        map.put("totalChargeWeight", acthalChargeWeight);

        Integer sellerPackageQty = null == inboundOrder.getOrderPackageQty() ? 0 : inboundOrder.getOrderPackageQty();
        map.put("sellerPackageQty", sellerPackageQty);
        Integer actualPackageQty = null == inboundOrder.getActualOrderPackageQty() ? 0 : inboundOrder.getActualOrderPackageQty();
        map.put("totalPackageQty", actualPackageQty);

        Integer sellerItemQty = null == inboundOrder.getOrderItemQty() ? 0 : inboundOrder.getOrderItemQty();
        map.put("sellerItemQty", sellerItemQty);
        Integer actualItemQty = null == inboundOrder.getActualItemCount() ? 0 : inboundOrder.getActualItemCount();
        map.put("totalItemQty", actualItemQty);

        map.put("status", inboundOrder.getStatus());
        InboundEnumConstant.OrderStatus status = InboundEnumConstant.OrderStatus.valueOf(inboundOrder.getStatus());
        if (status != null) {
            map.put("statusDesc", status.getDesc());
        } else {
            map.put("statusDesc", "undefined");
        }

        return map;
    }

}
